<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

class QuanmeiController extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = [];

        $data['notify_url'] = $return['notifyurl'];
        $data['return_url'] = $return['callbackurl'];
        switch($this->config['paytype']){
            case 1:
                $data['trade_type'] = '1';
                break;
            case 2:
                $data['trade_type'] = '4';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['merchant_code'] = $this->config['mch_id'];
        $data['order_no'] = $return['orderid'];
        $data['amount'] = number_format($return['amount'],2,'.','');
        $data['create_time'] = date("Y-m-d H:i:s",time());
        $data['source_ip'] = getIP();
        $data['extras'] = 'wlf';

        $data['sign'] = $this->getSign($data);

        po($this->config['gateway'], $data);
    }

    private function getSign($data){
        ksort($data);

        $str = '';
        foreach($data as $k=>$v){
            if($k==='sign' || $v==='' || $v===null){
                continue;
            }
            $str .= "{$k}={$v}&";
        }

        $str .= "key=" . $this->config['signkey'];

        return md5($str);
    }

    //通知
    public function notifyurl(){
		//$this->debug_callback();
        if(!$this->checkip(getIP(),['103.208.34.20','103.208.34.31','103.208.34.32','103.208.34.33','156.234.228.162'])){
            exit("wrong");
        }
		
        $p = I("post.");

        //获取订单信息
        $orderid = $p['order_no'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        /*M("Order")->where(array("pay_orderid"=>$p['busi_order_no']))->save(array(

        ));*/

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        $sign2 = $this->getSign($p);

        if($sign !== $sign2){
            exit("sign_wrong");
        }
        if($p['trade_status'] !== 'success'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'success');
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}